package com.osfw.dbadminer.domain.database;


import com.osfw.dbadminer.enums.SQLStmtType;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class SqlExecuteResult extends HashMap<String, Object> {
    private static final long serialVersionUID = 1L;

    /**
     * 状态
     */
    public static final String CODE_TAG = "code";
    /**
     * 语句类型
     */
    public static final String TYPE_TAG = "type";
    /**
     * 语句
     */
    public static final String STMT_TAG = "stmt";
    /**
     * 信息
     */
    public static final String MSG_TAG = "msg";
    /**
     * 耗时（s）
     */
    public static final String TIMES_TAG = "times";
    /**
     * 数据列
     */
    public static final String TOTAL_TAG = "total";
    /**
     * 数据列
     */
    public static final String PAGE_TAG = "page";
    /**
     * 数据列
     */
    public static final String SIZE_TAG = "size";
    /**
     * 数据列
     */
    public static final String COL_TAG = "column";
    /**
     * 数据对象
     */
    public static final String DATA_TAG = "data";

    /**
     * 状态类型
     */
    public enum Status {
        /**
         * 成功
         */
        SUCCESS(200),
        /**
         * 警告
         */
        WARN(301),
        /**
         * 错误
         */
        ERROR(500);

        private final int value;

        Status(int value) {
            this.value = value;
        }

        public int value() {
            return this.value;
        }
    }

    /**
     * 初始化一个新创建的 SqlExecuteData 对象，使其表示一个空消息。
     */
    public SqlExecuteResult() {
    }


    /**
     * 初始化一个新创建的 RestfulResult 对象
     */
    public SqlExecuteResult(Status code, SQLStmtType type, String stmt, String msg, Double times, Long total, Integer page, Integer size, List<TableColDef> column, List<Map<String, Object>> data) {
        super.put(CODE_TAG, code.value);
        super.put(TYPE_TAG, type.name());
        super.put(STMT_TAG, stmt);
        super.put(MSG_TAG, msg);
        super.put(TIMES_TAG, times);
        super.put(TOTAL_TAG, total);
        super.put(PAGE_TAG, page);
        super.put(SIZE_TAG, size);
        super.put(COL_TAG, column);
        super.put(DATA_TAG, data);
    }

    public static SqlExecuteResult success(SQLStmtType type, String stmt, String msg, Double times) {
        return new SqlExecuteResult(Status.SUCCESS, type, stmt, msg, times, null, null, null, null, null);
    }
    public static SqlExecuteResult success(SQLStmtType type, String stmt, String msg, Double times, Long total, Integer page, Integer size, List<TableColDef> column, List<Map<String, Object>> data) {
        return new SqlExecuteResult(Status.SUCCESS, type, stmt, msg, times, total, page, size, column, data);
    }

    public static SqlExecuteResult warn(SQLStmtType type, String stmt, String msg, Double times) {
        return new SqlExecuteResult(Status.WARN, type, stmt, msg, times, null, null, null, null, null);
    }
    public static SqlExecuteResult warn(SQLStmtType type, String stmt, String msg, Double times, Long total, Integer page, Integer size, List<TableColDef> column, List<Map<String, Object>> data) {
        return new SqlExecuteResult(Status.WARN, type, stmt, msg, times, total, page, size, column, data);
    }

    public static SqlExecuteResult error(SQLStmtType type, String stmt, String msg, Double times) {
        return new SqlExecuteResult(Status.ERROR, type, stmt, msg, times, null, null, null, null, null);
    }
    public static SqlExecuteResult error(SQLStmtType type, String stmt, String msg, Double times, Long total, Integer page, Integer size, List<TableColDef> column, List<Map<String, Object>> data) {
        return new SqlExecuteResult(Status.ERROR, type, stmt, msg, times, total, page, size, column, data);
    }


    public String getStmt(){
        return (String)get(STMT_TAG);
    }

    public String getMsg(){
        return (String)get(MSG_TAG);
    }

    public Double getTimes(){
        return (Double)get(TIMES_TAG);
    }

    public List<TableColDef> getColumn(){
        return (List<TableColDef>) super.get(COL_TAG);
    }

    public List<Map<String, Object>> getData(){
        return (List<Map<String, Object>>) super.get(DATA_TAG);
    }


    /**
     * 当前数据是否警告返回
     */
    public boolean isWarn(){
        int code = (int) super.get(CODE_TAG);
        if(code == Status.WARN.value){
            return true;
        }
        return false;
    }

    /**
     * 当前数据是否错误返回
     */
    public boolean isError(){
        int code = (int) super.get(CODE_TAG);
        if(code == Status.ERROR.value){
            return true;
        }
        return false;
    }

}
